package zjut.raymond.collectorplus.adapter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import zjut.raymond.collectorplus.R;
import zjut.raymond.collectorplus.pojo.ReferencePoint;

public class ReferencePointListAdapter extends RecyclerView.Adapter<ReferencePointListAdapter.MyViewHolder> {
    List<ReferencePoint> referencePoints = new ArrayList<>();
    private Context context;

    public ReferencePointListAdapter() {

    }

    public ReferencePointListAdapter(Context context) {
        this.context = context;
    }

    public void setReferencePoints(List<ReferencePoint> referencePoints) {
        this.referencePoints = referencePoints;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
//        View itemView = layoutInflater.inflate(R.layout.my_recyclerview_item, parent, false);
        View itemView = layoutInflater.inflate(R.layout.my_recyclerview_item_card, parent, false);
        return new MyViewHolder(itemView);
        //return null;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        final ReferencePoint referencePoint = referencePoints.get(position);
        if (referencePoint.getStatus() == 1) {
            holder.constraintLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.colorScanned));
        } else {
            holder.constraintLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.colorTextBackground));
        }

        holder.tvOrder.setText(String.valueOf(position + 1));
        holder.tvId.setText("参考点id: " + referencePoint.getId());
        holder.tvCoord.setText("参考点坐标: " + referencePoint.getCoord());

        holder.itemView.setOnClickListener(v -> {
            Log.d("TAG", "onClick: " + referencePoint.getId() + " " + referencePoint.getCoord());
            Bundle b = new Bundle();
            b.putInt("rp_id", referencePoint.getId());
            b.putString("rp_coord", referencePoint.getCoord());
            NavController controller = Navigation.findNavController(v);
            controller.navigate(R.id.action_mainFragment_to_detailFragment, b);
        });
    }


    @Override
    public int getItemCount() {
        return referencePoints.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvOrder, tvId, tvCoord;
        ConstraintLayout constraintLayout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            constraintLayout = itemView.findViewById(R.id.cl_item);
            tvOrder = itemView.findViewById(R.id.tv_order);
            tvId = itemView.findViewById(R.id.tv_rp_id);
            tvCoord = itemView.findViewById(R.id.tv_rp_coord);
        }
    }
}
